package org.example.servlet_01.request;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Enumeration;

/**
 * 获取请求头数据各个属性
 */

@WebServlet("/RequestDemo2")
public class RequestDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取所有请求头名称
        Enumeration<String>headerNames = request.getHeaderNames();
        while(headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            //根据名称获取请求头的值
            String value = request.getHeader(name);
            System.out.println(name + ":" + value);
        }
        //根据user-agent来处理浏览器兼容问题
        String agent = request.getHeader("user-agent");
        if(agent.contains("Chrome")){
            System.out.println("您使用的是Chrome浏览器");
        }
        else if(agent.contains("FireFox")){
            System.out.println("您使用的是FireFox浏览器");
        }
        else{
            System.out.println("您使用的是其他浏览器");
        }
        //根据referer来防盗链
        String referer = request.getHeader("referer");
        System.out.println("referer："+referer);
        if(referer == null || referer.contains("bdp1")){
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<h1>正常播放中......</h1>");
        }
        else{
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<a href='http://localhost:8080/bdp1/'>想看电影吗？来官网吧</a>");
        }
    }
}
